# change by hasaki
# last change on 2019/04/23
# 这个之前不是我写的，是个菜鸡旧同事彩霞写的，高不可用，辣鸡，做尼玛的程序

class Matplot:
    def __init__(self):
        self.savePath=''           # 图片保存路径
        self.time=[]               # 时间序列

    def kline(self, quotes):
        """ K线图 quotes = [(date, open, high, low, close),(date, open, high,low, close),(...),(....),...]"""
        import matplotlib.pyplot as plt
        import matplotlib.finance as mpf
        fig, ax = plt.subplots(figsize=(20,15))
        fig.subplots_adjust(bottom=0.5)
        mpf.candlestick_ohlc(ax,quotes,colorup='g',colordown='r',width=150,alpha=1)
        fig.savefig(self.savePath+'quote.jpg')
        plt.clf()
        plt.cla()
        plt.close()
        del plt
        del mpf
    
    def volume(self,data):
        '''生成成交量的直方图，或者折线图也是可以的'''
        import matplotlib.pyplot as plt
        plt.figure(figsize=(20, 15))
        time_=[i for i in range(len(data))]
        plt.plot(time_,data,color="orange")
        #plt.hist(data, bins=bin, normed=0, facecolor="orange", edgecolor="black", alpha=1)
        plt.savefig(self.savePath+'volume.jpg')
        plt.clf()
        plt.cla()
        plt.close()
        del plt
    '''
    def capitalList1(self, capitalList, pnlList):
        """总资产序列与净利润分布图 capitalList=[1,2,3,4]  pnlList=[1,2,3,4]"""
        plt.figure(figsize=(10, 16))
        pCapital = plt.subplot(2, 1, 1)
        pCapital.set_ylabel("capital")
        pCapital.plot(capitalList, color='r', lw=0.8)
        pPnl = plt.subplot(2, 1, 2)
        pPnl.set_ylabel("pnl")
        pPnl.hist(pnlList,  bins=10, color='c')
        plt.show()

    def mdd(self, balance, dt, drawdown,):
        """资产曲线和最大回撤图  balance:日线总资产 DataFrame dt: 日期列表，drawdown: 最大回撤序列"""
        plt.figure(figsize=(10, 16))
        pBalance = plt.subplot(2, 1, 1)
        pBalance.set_title('Daily Capital')
        balance.plot()  # legend=True
        pDrawdown = plt.subplot(2, 1, 2)
        pDrawdown.set_title('Drawdown')
        x1 = dt
        y1 = drawdown
        pDrawdown.fill_between(x1, y1)
        plt.show()

    def price(self, balance_, closePrice, dt):
        """资产曲线和价格走势对比图 balance_: 日净值序列,closePrice: 日收盘价序列, dt: 日期列表"""
        f3 = plt.figure(figsize=(10, 16))
        ax1 = f3.add_subplot(111)
        y1 = balance_
        y2 = closePrice
        x1 = dt
        ax1.plot(x1, y1, 'r-', label="Capital", linewidth=2)
        ax1.grid(False)
        ax2 = ax1.twinx()
        ax2.plot(x1, y2, 'b-', label="Price", linewidth=1)
        ax1.legend(loc='upper right')
        ax2.legend(loc='upper left')
        plt.show()

        '''